Just dumping it here by hand so I can learn, 99% content here taken from links below:
Ref: Rust by example , Comprehensive rust , Rust Playground
fn main() {
// println!, compile-time code generator before compilation.
println!("Hello world");
}
# compile this file in terminal by running:
rustc main.rs
// singleline comment
/* block level comment */
/// Generate library docs for following item.
//! Generate library docs for enclosing item.
E.g: https://doc.rust-lang.org/rust-by-example/meta/doc.html
Defined macros from std::fmt
.
format!
: write formatted text to String
print!
: same as format!
but the text is printed to the console (io::stdout).println!
: same as print!
but a newline is appended.eprint!
: same as print!
but the text is printed to the standard error (io::stderr).eprintln!
: same as eprint!
but a newline is appended.println!("day {}", 1);
let pi = 3.1415292;
println!("pi is {pi:.3}")
// 3.142
println!("Base 10: {}", 2); // 2
println!("Base 2: {:b}", 2); // 10 - binary
// b: binary base 2
// o: octal base 8
// x: hexadecimal base 16
println!(
"{0}: what's up? {1} replied, 'Nothing much, what about you? {0}'",
"John", "Susan"
)
println!("{person1}: what's up? {person2} replied, 'Nothing much, what about you? {person1}'", person1="John", person2="Susan")
println!("{:>10}", "John")
// Result, min width of 10 characters including text within:
// John
println!("{name:>5}", name="John")
// John
println!("{10}")
println!("{balance:0>5}", balance=6)
println!("{balance:X<5}", balance=7)
// 000006
// 7XXXXX
println!("{balance:Y<width$}", balance = 7, width = 2);
// 7Y
// or
let width = 2;
println!("{balance:Y<width$}")
println!("Custom {}", Structure(3))
By default, std::fmt
only available for the standard implementation, the rest will need to implement
it. Start debugging in println
by using {value:?}
.
// Wouldn't be able to print this one by default - custom type
struct UnPrintable(i32);
// Using default derive method
#[derive(Debug)]
struct Computer<'a> {
name: &'a str,
model: u32,
}
fn main() {
let zephyrus = Computer {
name: "Test",
model: 2014,
};
// Debug Normal print
println!("{zephyrus:?}");
// Debug Pretty print
println!("{zephyrus:#?}");
}
The & indicates a reference, meaning Computer does not own the string but only borrows it. The 'a is a lifetime parameter, ensuring that the reference to the string remains valid for at least the lifetime 'a.
use std::fmt; // Import `fmt`
// A structure holding two numbers. `Debug` will be derived so the results can
// be contrasted with `Display`.
#[derive(Debug)]
struct MinMax(i64, i64);
// Implement `Display` for `MinMax`.
impl fmt::Display for MinMax {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Use `self.number` to refer to each positional data point.
write!(f, "({}, {})", self.0, self.1)
}
}
// Define a structure where the fields are nameable for comparison.
#[derive(Debug)]
struct Point2D {
x: f64,
y: f64,
}
// Similarly, implement `Display` for `Point2D`.
impl fmt::Display for Point2D {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// Customize so only `x` and `y` are denoted.
write!(f, "x: {}, y: {}", self.x, self.y)
}
}
use std::fmt;
#[derive(Debug)]
struct Complex {
real: f32,
imag: f32,
}
impl fmt::Display for Complex {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{0} + {1}i", self.real, self.imag)
}
}
fn main() {
let c1 = Complex {
real: 3.3,
imag: 7.2,
};
println!("Print output:\n{c1}");
println!("\nDebug output:\n{c1:#?}");
}
/*
Print output:
3.3 + 7.2i
Debug output:
Complex {
real: 3.3,
imag: 7.2,
}
*/
#[allow(dead_code)]
is an attribute which only applies to the module after it.
By default, variable is immutable, mut
to let it mutable.
Built-in types and syntax for literal values
Types | Literals | |
---|---|---|
Signed integers | i8, i16, i32, i64, i128, isize | -10, 0, 1_000, 123_i64 |
Unsigned integers | u8, u16, u32, u64, u128, usize | 0, 123, 10_u16 |
Floating point numbers | f32, f64 | 3.14, -10.0e20, 2_f32 |
Unicode scalar values | char | 'a', '$', '∞' |
Booleans | bool | true, false |
Types have widths as follows: |
iN
, uN
, fN
are N bits wide,isize
and usize
are the width of a pointer,char
is 32 bits wide,bool
is 8 bits wide.